﻿using System.Collections;
using Com.Enemy.EnemySystem.Tools;
using Script.Com.Enemy.EnemySystem.EnemyAI;
using Script.Com.Human.Enemy.EnemySystem.EnemyStateAI;
using Script.EnemySystem.EnemyState;
using UnityEngine;

namespace Script.Com.Enemy.EnemySystem
{
    public class GuardController : EnemyControllerBase
    {
        private GuardStateState guardStateState;
        
        private bool getToMuchDamage;
        private float checkTime; 
            
        protected override void Awake()
        {
            base.Awake();
            guardStateState = gameObject.AddComponent<GuardStateState>();
        }

        protected override void MonsterAI()
        {
            if (!body.Target || body.Data.State == EnemyState.Hurt || body.Data.State == EnemyState.Defensing)
            {
                return;
            }
            
            if (body.IsPlayerInArea)
            {
                float distance = Vector2.Distance(transform.position, body.Target.position);
                if (distance < body.Data.MeleeAttackDistance)
                {
                    rb.velocity = Vector2.zero;
                    //近身攻击
                    NormalAttack();
                }else if (body.Data.State == EnemyState.Idle || body.Data.State == EnemyState.Walk)
                {
                    followPathAi.FollowPlayer();
                    guardStateState.PlayState(EnemyState.Walk);
                }else if (distance > body.Data.MeleeAttackDistance && body.Data.State == EnemyState.NormalAttack)
                {
                    guardStateState.InitChaseVar();
                }

            }
        }

        /// <summary>
        /// 发动条件：
        /// 1.当普通攻击冷却结束
        /// 2.未处于攻击状态（追击和普击）
        /// </summary>
        protected override void NormalAttack()
        {
            if (body.CoolDownSystem.HadColdDown(EnemyState.NormalAttack) && body.Data.State != EnemyState.NormalAttack && body.Data.State != EnemyState.ChaseAttack)
            {
                guardStateState.PlayState(EnemyState.NormalAttack);
            }
        }
    }
}